package cn.agiledata.bank.linkbank.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.logic.query.BankOrder;
import cn.agiledata.bank.linkbank.logic.query.Order;
import cn.agiledata.bank.linkbank.logic.query.OrderQueryByOrderIdRangeRequest;
import cn.agiledata.bank.linkbank.logic.query.OrderQueryByTimeRangeRequest;
import cn.agiledata.bank.linkbank.logic.query.OrderQueryResponse;
import cn.agiledata.bank.linkbank.logic.query.OrderQuerySendable;
import cn.agiledata.bank.linkbank.logic.query.SingleOrderQueryRequest;

/**
 * @author maxianbo
 * 
 */
public class BankQueryServiceImpl implements BankQueryService {
    private Map singleRequestMapping = new HashMap();

    private Map orderIdRangeRequestMapping = new HashMap();

    private Map timeRangeRequestMapping = new HashMap();

    public void setSingleRequestMapping(Map requestMapping) {
        this.singleRequestMapping = requestMapping;
    }

    public void setOrderIdRangeRequestMapping(Map requestMapping) {
        this.orderIdRangeRequestMapping = requestMapping;
    }

    public void setTimeRangeRequestMapping(Map requestMapping) {
        this.timeRangeRequestMapping = requestMapping;
    }

    private SingleOrderQueryRequest getSingleOrderQueryRequest(String bankCode) {
        return (SingleOrderQueryRequest) this.singleRequestMapping
                .get(bankCode);
    }

    private OrderQueryByOrderIdRangeRequest getOrderQueryByOrderIdRangeRequest(
            String bankCode) {
        return (OrderQueryByOrderIdRangeRequest) this.orderIdRangeRequestMapping
                .get(bankCode);
    }

    private OrderQueryByTimeRangeRequest getOrderQueryByTimeRangeRequest(
            String bankCode) {
        return (OrderQueryByTimeRangeRequest) this.timeRangeRequestMapping
                .get(bankCode);
    }

    public BankOrder queryOrder(String bankCode, String orderId)
            throws LinkBankException {
        SingleOrderQueryRequest queryRequest = getSingleOrderQueryRequest(bankCode);
        if (queryRequest == null)
            return null;
        queryRequest.setOrderId(orderId);
        queryRequest.validate();
        if (queryRequest instanceof OrderQuerySendable) {
            OrderQueryResponse orderQueryResponse = ((OrderQuerySendable) queryRequest)
                    .send();
            Order[] orders = orderQueryResponse.getOrders();
            if (orders.length > 0)
                return (BankOrder) orders[0];
        }
        return null;
    }

    public OrderQueryResponse queryOrdersByOrderIdRange(String bankCode,
            String status, String beginOrderId, String endOrderId,
            int maxPerPage, int pageNo) throws LinkBankException {
        OrderQueryByOrderIdRangeRequest queryRequest = getOrderQueryByOrderIdRangeRequest(bankCode);
        if (queryRequest == null)
            return null;
        queryRequest.setMaxPerPage(maxPerPage);
        queryRequest.setPageNo(pageNo);
        queryRequest.setStatus(status);
        queryRequest.setBeginOrderId(beginOrderId);
        queryRequest.setEndOrderId(endOrderId);
        queryRequest.validate();
        if (queryRequest instanceof OrderQuerySendable) {
            return ((OrderQuerySendable) queryRequest).send();
        }
        return null;
    }

    public OrderQueryResponse queryOrdersByTimeRange(String bankCode,
            String status, Date beginTime, Date endTime, int maxPerPage,
            int pageNo) throws LinkBankException {
        OrderQueryByTimeRangeRequest queryRequest = getOrderQueryByTimeRangeRequest(bankCode);
        if (queryRequest == null)
            return null;
        queryRequest.setMaxPerPage(maxPerPage);
        queryRequest.setPageNo(pageNo);
        queryRequest.setStatus(status);
        queryRequest.setBeginTime(beginTime);
        queryRequest.setEndTime(endTime);
        queryRequest.validate();
        if (queryRequest instanceof OrderQuerySendable) {
            return ((OrderQuerySendable) queryRequest).send();
        }
        return null;
    }
}
